package com.yicong.ants.ui.me;

import android.content.Intent;
import android.text.SpannableStringBuilder;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.Nullable;

import com.cchao.simplelib.core.DevHelper;
import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxBus;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cqyc.imview.TUICallKitUtils;
import com.yicong.ants.BuildConfig;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.databinding.SettingsActivityBinding;
import com.yicong.ants.manager.AppHelper;
import com.yicong.ants.manager.AppUpdateManager;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.ad.AdManager;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.utils.ConstantCodeUtils;
import com.yicong.ants.utils.GlideUtils;
import com.yicong.ants.utils.TextViewUtils;
import com.yicong.ants.view.ConfirmPopupWindow;

/**
 * 设置页
 */
public class SettingsActivity extends BaseTitleBarActivity<SettingsActivityBinding> implements View.OnClickListener {
    /**
     * 登出提示框
     */
    private ConfirmPopupWindow mDropOutPop;

    @Override
    protected int getLayout() {
        return R.layout.settings_activity;
    }

    @Override
    protected void initEventAndData() {
        setTitleBarVisible(false);
        AppHelper.transStatus(getWindow());
        mDataBind.tvName.setText("设置");

        if (!UserManager.isLogin()) {
            mDataBind.linLoggedOut.setVisibility(View.GONE);
        }
        UiHelper.setVisibleElseGone(mDataBind.deleteAccountBtn, UserManager.isLogin());
        mDataBind.version.setText(BuildConfig.VERSION_NAME);
        mDataBind.setClick(this);
        mDataBind.privacyField.setOnLongClickListener(v -> {
            AppHelper.showDevDialog2(thisActivity);
            return false;
        });
        loadAd();
        showCacheSize();
    }

    private void showCacheSize() {
        mDataBind.cacheSize.setText(GlideUtils.getCacheSize(mContext));
    }

    private void loadAd() {
        Logs.d("load ad settings");
        AdManager.showBanner(this, mDataBind.adContainer);
    }

    private void showLogOutWindow() {
        if (null == mDropOutPop) {
            mDropOutPop = new ConfirmPopupWindow(mContext);
        }
        mDropOutPop.setTxtTitle("提示");
        mDropOutPop.setTxtContent("是否退出当前账号?");
        mDropOutPop.setCall(this::logout);
        mDropOutPop.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AdManager.destroyBanner(this);
        if (null != mDropOutPop) {
            mDropOutPop.dismiss();
            mDropOutPop = null;
        }
    }

    @Override
    protected void onLoadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.lin_version_updating:
                BusinessHelper.loadIndexConfig(bool -> {
                    if (bool) {
                        AppUpdateManager.checkVersion2(thisActivity, false, ZzConfigs.mIndexInfo.getVersion());
                    } else {
                        showToast("已经是最新版本了");
                    }
                });

                break;
            case R.id.thirdPartShareBtn:
                ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_personal_third");
                break;
            case R.id.personalInfoBtn:
                ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_personal_list");
                break;
            case R.id.permissionBtn:
                ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_system");
                break;
            case R.id.agreement_field:
                ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_agreement");
                break;
            case R.id.privacy_field:
                if (AppHelper.isDebug() || DevHelper.isDev()) {
                    AppHelper.showDevDialog2(thisActivity);
                    return;
                }
                ZzConfigs.toWebActivityByUrlNotLogin(mContext, "ant_privacy");
                break;
            case R.id.lin_logged_out:
                showLogOutWindow();
                break;
            case R.id.clean_cache:
                Dialogs.showClearCache(thisActivity, intValue -> showCacheSize());
                break;
            case R.id.deleteAccountBtn:
                Router.turnTo(mContext, CancellationActivity.class).checkLogin(true).startForResult(102);
                break;
            case R.id.privilege:
                //系统权限管理
                startActivity(new Intent(this , PrivacyPrivilegeActivity.class));
        }
    }

    private void logout() {
        TUICallKitUtils.logOut();
        UserManager.logout();
        RxBus.get().postEvent(Constants.Event.Login_Out);
        showToast("已退出当前账号");
        setResult(ConstantCodeUtils.LOGGED_OUT);
        finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case ConstantCodeUtils.LOGGED_OUT:
                setResult(ConstantCodeUtils.LOGGED_OUT);
                finish();
                break;
            default:
                break;
        }
    }
}
